"""
yaml_tools.py


"""
aa = [{"title":"1.输入正确的用户名和密码,登录成功","username":"admin","password":"123456","expect":"签退"},
        {"title":"2.输入空的用户名和正确的密码密码,登录失败","username":"","password":"123456","expect":"登录失败，请检查您的成员名或密码是否填写正确。"},
        {"title":"3.输入正确的用户名和空的密码密码,登录失败","username":"admin","password":"","expect":"登录失败，请检查您的成员名或密码是否填写正确。"}
        ]
login_data = [{"title": "1.输入正确的用户名和密码，登录成功。", "username": "admin", "password": "123456",
                       "expect": "s-menu-superadmin"},
                      {"title": "2. 输入错误的的用户名和正确的密码,提示输出错误。", "username": "admin",
                       "password": "1234", "expect": "登录失败，请检查您的成员名或密码是否填写正确。"},
                      {"title": "3.输入空的用户名和空的密码，提示输出错误。", "username": "", "password": "",
                       "expect": "登录失败，请检查您的成员名或密码是否填写正确。"}
                      ]
# aa=[1,2,3,4]
# aa={"title":"1.输入正确的用户名和密码,登录成功","username":"admin","password":"123456","expect":"签退"}
import yaml
import os
from config.conf import BASE_DIR


class YamlTools:

    @staticmethod
    def get_project_path():
        cf = os.path.realpath(__file__)  # 获取当前文件的路径

        cd = os.path.dirname(cf)  # 获取当前文件目录的路径  caw
        cdd = os.path.dirname(cd)  # 获取 cd目录的上一级目录
        # print(cf,cd,cdd+"\\")
        return cdd + "\\"

    @staticmethod
    def read_yaml_file(path):
        path = YamlTools.get_project_path() + "\\testdata\\" + path  # 组合项目内的地址
        print(path)
        with open(path, "r", encoding='utf-8') as f:
            content = f.read()
            # 把yaml 文件 中的数据 转换为 python的 列表 或者 字典格式
            a = yaml.load(content, Loader=yaml.FullLoader)
            return a

    @staticmethod
    def write_yaml_file(data, filename):
        path = BASE_DIR + "\\testdata\\"
        filename = path + filename
        print(filename)
        with open(filename, 'w', encoding='utf-8') as file:
            yaml.dump(data, file, allow_unicode=True)


if __name__ == '__main__':
    # 创建login_data.yaml 并向其中写入测试数据
    YamlTools.write_yaml_file(login_data, "login_data.yaml")




    # aa = YamlTools.read_yaml_file('login_data.yaml')  # 给出项目内的相对地址。
    #
    # print(aa[0]["username"])